package com.weitu.majorHazard.informationRegistration.service.impl;

import com.weitu.accessory.util.AccessoryUtil;
import com.weitu.majorHazard.informationRegistration.entity.ContractRecord;
import com.weitu.majorHazard.informationRegistration.entity.InformationRegistration;
import com.weitu.majorHazard.informationRegistration.mapper.InformationRegistrationMapper;
import com.weitu.majorHazard.informationRegistration.service.InformationRegistrationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * Created by smp on 2017/4/24.
 */
@Service
public class InformationRegistrationServiceImpl implements InformationRegistrationService{
    private final InformationRegistrationMapper informationRegistrationMapper;

    @Autowired
    public InformationRegistrationServiceImpl(InformationRegistrationMapper informationRegistrationMapper){
        this.informationRegistrationMapper=informationRegistrationMapper;
    }

    @Override
    public int queryCount(Map<String, Object> map) {
        return this.informationRegistrationMapper.queryCount(map);
    }

    @Override
    public List<InformationRegistration> queryList(Map<String, Object> map) {
        return this.informationRegistrationMapper.queryList(map);
    }

    @Override
    @Transactional
    public boolean insertMajorHazardMessage(InformationRegistration informationRegistration) {
        int count=this.informationRegistrationMapper.insertMajorHazardMessage(informationRegistration);
        boolean flag= AccessoryUtil.saveAccerssoryInfo(informationRegistration.getId());
        boolean flag2=false;
        if(count==1)
            flag2=true;
        return flag&&flag2;
    }

    @Override
    @Transactional
    public boolean updateMajorHazardMessage(InformationRegistration informationRegistration, List<ContractRecord> contractRecordList) {
        this.informationRegistrationMapper.deleteContractRecord(informationRegistration.getId());
        if(contractRecordList.size()>0)
        this.informationRegistrationMapper.insertContractRecord(contractRecordList);
        boolean flag= AccessoryUtil.saveAccerssoryInfo(informationRegistration.getId());
       int count = this.informationRegistrationMapper.updateMajorHazardMessage(informationRegistration);
        boolean flag2=false;
        if(count==1)
            flag2=true;
        return flag&&flag2;
    }

    @Override
    public InformationRegistration queryInformationRegistrationById(String id) {
        return this.informationRegistrationMapper.queryInformationRegistrationById(id);
    }

    @Override
    public int deleteInformationRegistrationById(String id) {
        return this.informationRegistrationMapper.deleteInformationRegistrationById(id);
    }

    @Override
    public  List<ContractRecord> queryContractRecord(String messageId) {
        return this.informationRegistrationMapper.queryContractRecord(messageId);
    }
}
